/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameSignUpGPanel.java
 *
 * Created on 2009-9-27, 16:01:28
 */
package com.res0w.SMMS.ui.admin;

import com.res0w.SMMS.admin.Schedule;
import com.res0w.SMMS.factory.DataBaseOperate;
import com.res0w.SMMS.fuction.MakeId;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

/**
 *
 * @author res0w
 * @version 0.2
 * @since 2009-10-2
 */
public class GameSignUpGPanel extends javax.swing.JPanel {

    /** Creates new form GameSignUpGPanel */
    public GameSignUpGPanel() {
        initComponents();
        myInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        signUpTable = new javax.swing.JTable();
        academy = new javax.swing.JTextField();
        specialField = new javax.swing.JTextField();
        classes = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        submitBtn = new javax.swing.JButton();
        endSignUpBtn = new javax.swing.JButton();
        refreshBtn = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setFont(new java.awt.Font("微软雅黑", 0, 12));
        setPreferredSize(new java.awt.Dimension(950, 530));

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel1.setText("团体报名：");

        jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 12));
        jLabel2.setText("学院：");

        jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 12));
        jLabel3.setText("专业：");

        jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 12));
        jLabel4.setText("班级：");

        signUpTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},
                {"", "", "", ""},},
            new String[]{
                "学号", "姓名", "性别", "参报项目"
            }));
            jScrollPane1.setViewportView(signUpTable);
            signUpTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(tSignC3));
            signUpTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(tSignC4));

            academy.setFont(new java.awt.Font("微软雅黑", 0, 12));

            specialField.setFont(new java.awt.Font("微软雅黑", 0, 12));

            classes.setFont(new java.awt.Font("微软雅黑", 0, 12));

            jLabel5.setFont(new java.awt.Font("微软雅黑", 0, 18));
            jLabel5.setText("使用提示：");

            jTextArea1.setColumns(20);
            jTextArea1.setEditable(false);
            jTextArea1.setFont(new java.awt.Font("微软雅黑", 0, 13));
            jTextArea1.setRows(5);
            jTextArea1.setText("项目次序：\n项目次序输入类型只能是【数字】，按照比赛的循序进行递加\n若两个项目同时举行则项目次序一致。\n\n项目名称：\n项目从下拉菜单中选择\n\n举行时间：\n此项为项目的举行时间，输入格式为自由格式：\n如：【**年*月*日*时*日*时】\n或者【赛程第一天下午第*项】等可以表示时间次序均可\n\n举行地点：\n此项为项目的举行地点，输入格式为自由格式：\n如：【400米跑道】，【篮球场】等均可\n\n**【截止报名】\n当点击截止报名之后，将无非再进行【团体】或者【个人】\n报名，也无法修改；\n点击之后同时生成【运动员号】，并且弹出全体报名运动员名单\n日后再次查询，可再次点击【查询】");
            jScrollPane2.setViewportView(jTextArea1);

            jButton2.setText("全部重写");

            submitBtn.setText("提交");
            submitBtn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    submitBtnActionPerformed(evt);
                }
            });

            endSignUpBtn.setText("截止报名");
            endSignUpBtn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    endSignUpBtnActionPerformed(evt);
                }
            });

            refreshBtn.setText("刷新");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(60, 60, 60)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(academy, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(specialField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(classes, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 276, Short.MAX_VALUE)
                            .addComponent(refreshBtn)
                            .addGap(50, 50, 50)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel5)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(14, 14, 14)
                            .addComponent(endSignUpBtn)
                            .addGap(48, 48, 48)
                            .addComponent(jButton2)
                            .addGap(45, 45, 45)
                            .addComponent(submitBtn)
                            .addGap(45, 45, 45)))
                    .addGap(21, 21, 21))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(28, 28, 28)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(academy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3)
                                .addComponent(specialField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(classes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(refreshBtn)))
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(submitBtn)
                                .addComponent(endSignUpBtn)
                                .addComponent(jButton2))))
                    .addContainerGap())
            );
        }// </editor-fold>//GEN-END:initComponents

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        // TODO add your handling code here:
        submitData();
    }//GEN-LAST:event_submitBtnActionPerformed

    private void endSignUpBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endSignUpBtnActionPerformed
        // TODO add your handling code here:
        endSignUp();
    }//GEN-LAST:event_endSignUpBtnActionPerformed

//Create By Author
    private void submitData() {
        sc.academy = academy.getText();
        sc.specialField = specialField.getText();
        sc.classes = classes.getText();
        int i = 0;
        while (signUpTable.getValueAt(i, 0) != "") {
            sc.sId = signUpTable.getValueAt(i, 0).toString();
            sc.sName = signUpTable.getValueAt(i, 1).toString();
            if (signUpTable.getValueAt(i, 2).toString() == "男") {
                sc.sex = 0;
            } else if (signUpTable.getValueAt(i, 2).toString() == "女") {
                sc.sex = 1;
            }
            sc.joinItems = signUpTable.getValueAt(i, 3).toString();
            i++;
            sc.cacheListUpdate(false);
        }
        sc.onlyExe4CacheSchedule();
    }

    private void endSignUp() {
        try {
            //随机生成运动员号码
            MakeId make = new MakeId();
            make.autoMakeAthleteId();
            //设置按钮不可用
            endSignUpBtn.setEnabled(false);
            submitBtn.setEnabled(false);

            switch4EndSignUp(false);
            //弹出ShowSignUpList窗口
            ShowSignUpList show = new ShowSignUpList();
            show.setLocationRelativeTo(null);
            show.setVisible(true);

        } catch (SQLException ex) {
            Logger.getLogger(GameSignUpGPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void myInit() {
        //读取报名信息，是否截止
        if (load4EndSignUp() == 0) {
            endSignUpBtn.setEnabled(true);
            submitBtn.setEnabled(true);
        } else if (load4EndSignUp() == 1) {
            endSignUpBtn.setEnabled(false);
            submitBtn.setEnabled(false);
        }
    }

    private void switch4EndSignUp(boolean onOff) {
        String sql = null;
        if (onOff) {
            sql = "update setting_normal set endSignUp=0 ";
        } else {
            sql = "update setting_normal set endSignUp=1 ";
        }
        DataBaseOperate db = new DataBaseOperate();
        db.dbExe(sql);
        db.dbClosed();
    }

    private int load4EndSignUp() {
        int result = 0;
        try {
            ArrayList rsList = new ArrayList();
            String sql = "select endSignUp from setting_normal";
            DataBaseOperate db = new DataBaseOperate();
            rsList = db.dbQuerySolo(sql, true);
            result = Integer.parseInt(rsList.get(0).toString());
        } catch (SQLException ex) {
            Logger.getLogger(GameSignUpGPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    private JComboBox tSignC3 = new JComboBox(new Object[]{
                "男",
                "女",});
    private Schedule sc = new Schedule();
    private JComboBox tSignC4 = new JComboBox(sc.readFullItems());
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField academy;
    private javax.swing.JTextField classes;
    private javax.swing.JButton endSignUpBtn;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton refreshBtn;
    private javax.swing.JTable signUpTable;
    private javax.swing.JTextField specialField;
    private javax.swing.JButton submitBtn;
    // End of variables declaration//GEN-END:variables
}
